<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>数组方法</title>
</head>
<body>
<script>
    // from方法
    function add(){
        console.log(arguments) //arguments是一个伪数组
        //将arguments转化为数组,方式1
        let arr = Array.from(arguments);
        console.log(arr)
        //将arguments转化为数组,方式2
        console.log([...arguments])
    }
    add(1,2,3,4)

    //from第二个参数 //将一个数组对象得到某个元素提取出来转化为单独的数组
    studentList=[{name:'张三',age:23},{name:'李四',age:22}]
    let ageArr = Array.from(studentList,item => item.age) 
    console.log(ageArr)
    //构建数组的第二种方式
    let strArr = Array.of(1,2,3,"hello","python")
    console.log(strArr)


    //数组的遍历
    //定义一个字符串数组
    let fruits = ["apple","banana","orange","grape","pear","watermelon","pineapple","mango"]
    //forEach方法
    fruits.forEach(function(item,index,arr){
        console.log(item,index) //item是数组元素，index是元素的索引
        console.log(arr) //arr就是数组本身
    })
    //通过生成遍历器去遍历数组
     let iterator = fruits.keys() //返回一个遍历器对象，可以用for...of循环遍历
     console.log(iterator)
     for(let key of iterator){
        console.log("key:{},value:{}",key,fruits[key]);   
     }
     let fruitsValues = fruits.values()
     for(let value of fruitsValues){
         console.log(value)

     }

     let fruitsList = fruits.entries();
     for(let [index,value] of fruitsList){
         console.log("index:{},value:{}",index,value);
     }

    //ES5判断是否包含某个元素
    if(fruits.indexOf("apple") !== -1){
        console.log("包含apple")
    } 
    //ES6判断是否包含某个元素
    if(fruits.includes("apple")){
        console.log("包含apple")
    } 
   

</script>
</body>
</html>